package com.HQCourse.vod.vod.service.impl;

import com.HQCourse.Exception.HQCourseException;
import com.HQCourse.vod.vod.listener.ExcelListener;
import com.HQCourse.vod.vod.mapper.SubjectMapper;
import com.HQCourse.vod.vod.service.SubjectService;
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.HQCourse.model.vod.Subject;
import com.example.HQCourse.vo.vod.SubjectEeVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author zww
 * @since 2023-06-24
 */
@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper,Subject> implements SubjectService {
  @Autowired
  private ExcelListener excelListener;
    @Override
    public List<Subject> selectListById(Long id) {
        QueryWrapper<Subject> wrapper=new QueryWrapper<>();
        wrapper.eq("parent_id",id);
        List<Subject> subjects = baseMapper.selectList(wrapper);
        for (Subject subeject:subjects)
        {
            Long subejectId=subeject.getId();
            boolean isChild=this.isChild(subejectId);
            subeject.setHasChildren(isChild);
        }
        return subjects;
    }
    private Boolean isChild(Long id){
        QueryWrapper<Subject> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("parent_id",id);
        Integer integer = baseMapper.selectCount(queryWrapper);
        return integer>0;

    }

    /**
     * 课程分类导出
     * @param response
     */

    @Override
    public void exportData(HttpServletResponse response) {
        /**  subjectVo.setId(subject.getId());
         subjectVo.setParentId(subject.getParentId());
         //spring的工具类直接将一个对象的值赋值到另一个对象 BeanUtils.copyProperties(subject,subjectVo);
         */
        try {
            //设置下载信息
            response.setContentType("application/vod.ms-excel");
            //处理字符集乱码
            response.setCharacterEncoding("utf-8");
           String fileName = URLEncoder.encode("课程分类","utf-8");
            response.setHeader("Content-disposition","attachment;filename="+fileName+".xlsx");
            List<Subject> subjects = baseMapper.selectList(null);
            List<SubjectEeVo> list=new ArrayList<>();
            for (Subject subject:subjects) {SubjectEeVo subjectVo = new SubjectEeVo();
               BeanUtils.copyProperties(subject,subjectVo);
                list.add(subjectVo);
            }
            EasyExcel.write(response.getOutputStream(), SubjectEeVo.class).sheet("课程分类").doWrite(list);
            } catch (Exception e) {
            throw new RuntimeException(e);
            }
    }

    @Override
    public void inport(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(),
                    SubjectEeVo.class,excelListener)
                    .sheet().doRead();

        } catch (IOException e) {
            throw new HQCourseException(20001,"导入失败");

        }

    }
}
